﻿using System;
using System.Data;
using System.Collections.Generic;
using SERP.DAO;
namespace SERP.Controllers{
public class UsersController : UsersControllerBase{
	public UsersController()
		: base()
	{
	}	
	private DataTable _Table;
	private UsersEntity oUsers;	
	public UsersEntity UsersObject{
		get{
			return oUsers;
		}
		set{
			oUsers=value;
		}
	}
	public DataTable Table{
		get{
			if(_Table==null&&AllowCaching){				
				_Table=Cache.Get<DataTable>(CACHE_KEY_DS);
			}
			return _Table;
		}
		set{
			_Table=value;
		}
	}
	public void LoadAllDSUsers(bool clearCache){
		bool bForce=clearCache;
		if(bForce||_Table==null)
		{
			DataSet ds=Service.SelectAllDS();
			_Table=ds.Tables[0];
			AddIndexToTable(_Table);
			if(AllowCaching)
				AddCache(CACHE_KEY_DS,_Table);			
		}				
	}	
	public void AddIndexToTable(){
		AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		ResetTableIndex(_Table);
	}
	public void LoadObject(Int32 UserId){
		UsersEntity oLoad=FindUsers(UserId);
		if(oLoad==null){
			LoadAllUsers(true);
			oUsers=FindUsers(UserId);
		}
		else{
			oUsers=oLoad;
		}
	}
	public bool AddNew(){
		if(oUsers==null)return false;
		bool result = Service.Save(oUsers);
		if(result){
			AddObjectToCache(oUsers);
		}
		return result;
	}
	public bool Update(){
		if(oUsers==null)return false;
		return Service.Update(oUsers);	
	}
	public bool Delete(){
		if(oUsers==null)return false;		
		bool retvalue = Service.Delete(oUsers);
		if(retvalue){			
			RemoveObjectFromCache(oUsers);
		}
		return retvalue;
	}	
	public bool DeleteByID(Int32 UserId){
		bool retvalue = Service.DeleteByID(UserId);		
		if(retvalue){
			UsersEntity oDeleted=FindUsers(UserId);
			RemoveObjectFromCache(oDeleted);
		}
		return retvalue;
	}	
	public void ResetObject(){
		this.oUsers = new UsersEntity();
	}
	public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.UsersObject,row);
    }
    public UsersEntity LoadUsers(Int32 UserId){			
		UsersEntity o=Service.SelectOne(UserId);		
		return o;
	}
	#region Cached
    private static List<UsersEntity>_ListUsers;
	private static bool _AllowCaching=true;
	public static bool AllowCaching{
		get{return _AllowCaching;}
		set{AllowCaching=value;}
	}	
	public static void ClearCache(){
			if(!_AllowCaching)return;
			Cache.Remove(CACHE_KEY_DS);
			Cache.Remove(CACHE_KEY_LS);
	}
	private const string CACHE_KEY_DS="UsersDS";
	private const string CACHE_KEY_LS="UsersLS";
	private void AddObjectToCache(UsersEntity newObject){
		if(!AllowCaching)return;
		GetCacheList().Add(newObject);
	}
	private void RemoveObjectFromCache(UsersEntity oRemove)
	{
		if(!AllowCaching)return;
		if(oRemove!=null)
		GetCacheList().Remove(oRemove);
	}
	private static List<UsersEntity>GetCacheList(){
		if(Cache==null)return default(List<UsersEntity>);
		List<UsersEntity>ls= Cache.Get<List<UsersEntity>>(CACHE_KEY_LS);
		if(ls==null){
			ls = new List<UsersEntity>();
			AddCache(CACHE_KEY_LS,ls);
		}
		return ls;
	}
	public List<UsersEntity>ListUsers{
		get{
			return LoadAllUsers(false);
		}
	}
	public static List<UsersEntity>ListUsersStatic{
		get{
			LoadAllUsers(false);
			return _ListUsers;
		}
	}
	public UsersEntity FindUsers(Int32 UserId){
		if(AllowCaching){
			_ListUsers=GetCacheList();
		}		
		UsersEntity oResult=_ListUsers.Find(delegate(UsersEntity oFind){
			bool result=true; 
			result&=oFind.UserId.Equals(UserId);
			return result;
		 });
		return oResult;
	}
	public static List<UsersEntity>LoadAllUsers(bool clearCache){
		_ListUsers = new List<UsersEntity>();
		if(AllowCaching){
			_ListUsers=GetCacheList();
		}
		if(_ListUsers==null||_ListUsers.Count==0)
			clearCache=true;
		if(!clearCache)return _ListUsers;
		_ListUsers=new List<UsersEntity>(Service.SelectAll());		
		if(AllowCaching)
			AddCache(CACHE_KEY_LS,_ListUsers);
		return _ListUsers;
	}
	#endregion
}
}
